home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0066_Huge Pointers in DOS.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  4KB  |  162 lines

  1. {
  2. Tel   : 7+(095) 333-9469
  3. E-Mail: vad@glas.apc.org
  4.  
  5. >I'm wondering if anyone can tell me whether dynamic arrays have the same
  6. >64Kbyte limit per field that static arrays do? If so can ayone suggest away
  7. >to store data in memory that is sure to exceed this limit?
  8.  
  9. The first way is known to all serious TP programmers.
  10. You could create large fragmental structure by control block.
  11. Example:
  12. }
  13.  
  14. Type
  15.     Large =  Array [1 .. 65520] of Byte;
  16.     PLarge = ^Large;
  17.     Huge = Array [1 .. 16380] of Pointer; { control block }
  18.     PHuge = ^Huge;
  19. Var
  20.    Hg : PHuge;
  21.    MaxRow , MaxCol , r , c : Word;
  22.  
  23. begin
  24.      Repeat ReadLn(MaxRow , MaxCol)
  25.      Until (MaxCol > 0) and (MaxCol <= 65520) and
  26.                               (MaxRow > 0) and (MaxRow <= 16380);
  27.      GetMem(Hg , Sizeof(Pointer) * MaxRow);
  28.      For r := 1 to MaxRow do GetMem(Hg^[r] , MaxCol);
  29.      For r := 1 to MaxRow
  30.      do For c := 1 to MaxCol
  31.         do Hg^[r]^[c] := Random(255);
  32. end.
  33.  
  34. {
  35. This method creating huge structures is using Borland in object named
  36. TColection.
  37. This method has two defects.
  38.  * You must use big control structure consists of pointers.
  39.  * You haven't garant of unfrugmental of this structure.
  40. This method have two advantages.
  41.  * You can use standart pascal procedures & macros for operating with
  42.    this structure (As Move, FillChar ets).
  43.  * You can use standart TP heap menager for creating huge
  44.    structure.
  45.  
  46. The second way for creating huge structure. It's my own method.
  47. I use DOS memory menager.  I can create realy huge unfragmental
  48. structure. It's more complicated way. I can't use standart TP
  49. procedure & macros for operating with that structure, because
  50. this structure greater than 64Kb. For remove huge structure I
  51. write some procedure, function & inline macros. I used standart TP
  52. heap menager for allocation small control structure and used
  53. DOS memory menager for allocation huge data structure.
  54. Example:
  55. {$M 4096,10000,10000 <- It's very important option. Without
  56. this option Your program should use all DOS memory for his TP
  57. heap. You should write Your realy heap size. Use TpStack from
  58. Turbo profesional for get this value.
  59. }
  60.  
  61. function dosAlloc (Size : Longint) : Pointer; assembler;
  62. asm 
  63.    mov   ax , word ptr Size 
  64.    mov   dx , word ptr Size[2] 
  65.    add   ax , 000Fh 
  66.    adc   dx , 0 
  67.    mov   bx , 0010h 
  68.    div   bx 
  69.    mov   bx , ax 
  70.    mov  ah , 48h 
  71.    int  21h 
  72.    jC  @@Error 
  73.    mov  dx , ax 
  74.    xor  ax , ax 
  75.    jmp  @@Exit 
  76. @@Error: 
  77.    xor  ax , ax 
  78.    xor  dx , dx 
  79. @@Exit: 
  80. end;
  81.  
  82. function dosFree (Var P) : Boolean; assembler; 
  83. asm 
  84.    les   di , P 
  85.    mov   ax , es:[di] 
  86.    mov   dx , es:[di][2] 
  87.    mov   cl , 4 
  88.    shr   ax , cl 
  89.    add   ax , dx 
  90.    or    ax , ax
  91.    jZ    @@Exit 
  92.    mov   es , ax 
  93.    mov   ah , 49h 
  94.    int   21h 
  95.    jNC   @@Continue 
  96.    mov   ax , False 
  97.    jmp   @@Exit 
  98. @@Continue: 
  99.    xor    ax , ax 
  100.    les    di , P 
  101.    mov    es:[di] , ax 
  102.    mov    es:[di][2] , ax 
  103.    mov    ax , True 
  104. @@Exit:
  105. end; 
  106.  
  107. function dosResize (P : Pointer; NewSize : Longint) : Boolean; assembler; 
  108. asm 
  109.    mov   ax , False 
  110.    mov   bx , word ptr P[2] 
  111.    mov   es , bx 
  112.    or    bx , word ptr P 
  113.    jZ    @@Exit 
  114.    mov   ax , word ptr NewSize 
  115.    mov   dx , word ptr NewSize[2] 
  116.    mov   bx , 16 
  117.    div   bx 
  118.    mov   bx , ax 
  119.    or    dx , dx 
  120.    jZ    @@Margin 
  121.    inc   bx 
  122. @@Margin: 
  123.    mov  ah , 4Ah 
  124.    int  21h 
  125.    jC  @@Error 
  126.    mov  ax , True 
  127.    jmp  @@Exit 
  128. @@Error: 
  129.    mov  ax , False 
  130. @@Exit: 
  131. end; 
  132.  
  133. function dosAvail : Longint; assembler; 
  134. asm 
  135.    mov  bx , 0FFFFh
  136.    mov  ah , 48h 
  137.    int  21h 
  138.    mov  ax , 16 
  139.    mul  bx 
  140. end; 
  141.  
  142. Var 
  143.    Hg : Pointer; 
  144.  
  145. begin 
  146.      Hg := dosAlloc(123456); 
  147. end.         
  148.  
  149. {
  150.    I don't want send more than 16Kb of my library. Because I don't test
  151.    all procedure & function. But I can give You my idea.
  152.    This way has one defect.
  153.    * You should rewrite all low level memory access (As Move,
  154.       FillChar ets.).
  155.    This way has four advantages.
  156.    * You can use all DOS memory functions: Allocation, Resize,
  157.      Free (It's more powerfull that TP).
  158.    * You recive paragraph align pointer.
  159.    * You get unfragmental memory block.
  160.    * You don't use big control structure.
  161. }
  162.